<template>
  <!-- 新建盘点 -->
  <div>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="盘点单号">
          <el-input v-model="queryParam.billNumber" placeholder="请输入盘点单号" clearable></el-input>
        </el-form-item>
        <el-form-item label="盘点类型">
          <el-select v-model="queryParam.labelType" placeholder="请选择" clearable>
            <el-option v-for="item in [
              { value: 0, label: '档案盘点' },
              { value: 1, label: '盒盘点' },
            ]" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="rest">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增盘点</el-button>
        </el-form-item>
      </el-form>
      <!-- </el-col> -->
      <el-row :gutter="24">
        <el-col :span="12"> </el-col>
      </el-row>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" row-key="billId" border style="width: 100%" height="55vh">
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <span v-if="item.prop === 'labelType'">
              <el-tag v-if="scope.row.labelType === 0">档案盘点</el-tag>
              <el-tag v-if="scope.row.labelType === 1">盒盘点</el-tag>
            </span>
            <span v-else-if="item.prop === 'inventoryTool'">
              <el-tag v-if="scope.row.inventoryTool === 0">人工</el-tag>
              <el-tag v-if="scope.row.inventoryTool === 2">盘点车</el-tag>
              <el-tag v-if="scope.row.inventoryTool === 3">密集架</el-tag>
            </span>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-edit" link size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm class="button10Left" title="确定要删除吗？" @confirm="() => handleDel(row)">
              <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
            </el-popconfirm>
            <el-button v-if="row.inventoryTool === 3" class="button10Left" link size="mini"
              @click="inventoryCheck(row)">盘点</el-button>
            <el-button v-if="row.inventoryTool === 0" icon="el-icon-download" class="button10Left" link size="mini"
              @click="exportExcel(row)">导出</el-button>
            <el-button v-if="row.inventoryTool === 0" icon="el-icon-upload2" class="button10Left" link size="mini"
              @click="importExcel(row)">导入</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <pagination ref="pagination" :totalCount="totalCount" @pagination="pagination"></pagination>
    </el-card>
    <new-inventory-dialog ref="newInventoryDialog" @ok="loadData" />
    <input ref="excelUploadInput" type="file" accept=".xlsx, .xls" style="display: none" @change="uploadChange" />
  </div>
</template>

<script>
import { pageList, del, mjjInventory, exportExcel, importData } from '@/api/rfid/inventoryBill'
import newInventoryDialog from './dialog/newInventoryDialog.vue'
export default {
  name: 'createInventoryCheck',
  components: { newInventoryDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {
        inventoryStatus: 0,
      },
      tableData: [],
      tableProps: [
        { title: '盘点单号', prop: 'billNumber', width: '180', checked: true },
        { title: '盘点主题', prop: 'billTitle', width: '', checked: true },
        { title: '盘点类型', prop: 'labelType', width: '', checked: true },
        { title: '盘点工具', prop: 'inventoryTool', width: '', checked: true },
        { title: '录入时间', prop: 'createTime', width: '180', checked: true },
        { title: '备注', prop: 'note', width: '', checked: true },
      ],
      billNumber: '',
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    rest() {
      this.queryParam = {
        inventoryStatus: 0,
      }
      this.$refs.pagination.resetPagination()
    },
    loadData() {
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    handleAdd() {
      this.$refs.newInventoryDialog.add()
    },
    handleEdit(row) {
      this.$refs.newInventoryDialog.edit(row)
    },
    handleDel(row) {
      del(row.billNumber)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    inventoryCheck(row) {
      mjjInventory(row)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.$message.warning(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    exportExcel(row) {
      exportExcel(row.billNumber)
        .then((res) => {
          // 创建一个新的URL代表指定的File对象或Blob对象
          const url = window.URL.createObjectURL(new Blob([res.data]))
          // 创建一个a标签用于下载
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', row.billNumber + '.xlsx') // 下载文件名
          document.body.appendChild(link)
          link.click()
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    importExcel(row) {
      this.billNumber = row.billNumber
      this.$refs.excelUploadInput.click()
    },
    async uploadChange(event) {
      const files = event.target.files
      if (files.length > 0) {
        // 大于0，说明有文件要上传
        // 创建FormData对象
        const data = new FormData()
        // 添加文件
        data.append('file', files[0])
        data.append('billNumber', this.billNumber)
        // 调用接口
        await importData(data)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('盘点成功！')
              this.loadData()
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
          .finally(() => {
            this.$refs.excelUploadInput.value = ''
          })
      }
    },
  },
}
</script>

<style scoped lang="less">
.demo-form-inline {
  .el-form-item {
    margin-bottom: 0 !important;
  }
}
</style>
